package models;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.Query;

import org.hibernate.annotations.IndexColumn;

import controllers.StartJourney;

import play.data.validation.Required;
import play.db.jpa.JPA;
import play.db.jpa.Model;

@Entity
public class User extends Model {
	@Column(unique = true) 
	public String facebookID;

	private String name;
	private String profilPicture;
	private String profilPictureBig;	


	@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
	private List<Vehicle> vehicles;

	@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
	private List<Journey> journeyList;
	
	@OneToMany(mappedBy = "sender", cascade = CascadeType.ALL)
	private List<Message> sentMessages;
	
	@OneToMany(mappedBy = "receiver", cascade = CascadeType.ALL)
	private List<Message> receivedMessages;
	
	@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
	private List<Request> comingRequests;
	
	@OneToMany(mappedBy = "requester", cascade = CascadeType.ALL)
	private List<Request> sentRequests;

	public User(String facebookID, String name, String profilPicture, String profilPictureBig,
			List<Vehicle> vehicles, List<Journey> journeyList, List<Message> sentMessages, List<Message> receivedMessages,
			List<Request> comingRequests, List<Request> sentRequests) {
		super();
		this.facebookID = facebookID;
		this.name = name;
		this.profilPicture = profilPicture;
		this.profilPictureBig = profilPictureBig;
		this.vehicles = new ArrayList<Vehicle>();
		this.journeyList = new ArrayList<Journey>();
		this.sentMessages = new ArrayList<Message>();
		this.receivedMessages = new ArrayList<Message>();
		this.comingRequests = new ArrayList<Request>();
		this.sentRequests = new ArrayList<Request>();
	}

	
	public List<Journey> getJourneyList() {
		return Journey.find("user like ? order by id desc",this).fetch();
	}

	public void setJourneyList(List<Journey> journeyList) {
		this.journeyList = journeyList;
	}

	public List<Vehicle> getVehicles() {
		return vehicles;
	}

	public void setVehicles(List<Vehicle> vehicles) {
		this.vehicles = vehicles;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setProfilPicture(String profilPicture) {
		this.profilPicture = profilPicture;
	}

	public String getProfilPicture() {
		return profilPicture;
	}

	public void setProfilPictureBig(String profilPictureBig) {
		this.profilPictureBig = profilPictureBig;
	}

	public String getProfilPictureBig() {
		return profilPictureBig;
	}

	public User(String facebookID) {
		this.facebookID = facebookID;
	}

	public String getFacebookID() {
		return facebookID;
	}

	public void setFacebookID(String facebookID) {
		this.facebookID = facebookID;
	}

	public List<Message> getSentMessages() {
		return sentMessages;
	}


	public void setSentMessages(List<Message> sendedMessages) {
		this.sentMessages = sendedMessages;
	}


	public List<Message> getReceivedMessages() {
		return receivedMessages;
	}


	public void setReceivedMessages(List<Message> receivedMessages) {
		this.receivedMessages = receivedMessages;
	}


	public static User getByFacebookID(String facebookID) {
		return User.find("facebookID = ? ", facebookID).first();
	}
	
	public List<DriverJourney> getDriverJourneyList() {
		return DriverJourney.find("user like ?", this).fetch();
	}

	public List<HitchhikerJourney> getHitchhikerJourneyList() {
		return HitchhikerJourney.find("user like ?", this).fetch();
	}
	
	public Vehicle findVehicle(String plate){
		Vehicle vhc = null;
		
		for (Vehicle vehicle : this.getVehicles()) {
			if (vehicle.getPlate().equals(plate))
				vhc = vehicle;
		}
		return vhc;
	}

	public User addVehicle(User user, String plate, String trademark, String model, String color, String age){

		Vehicle vehicle = new Vehicle(user, plate, trademark, model, color, age);
		vehicle.save();
		vehicles.add(vehicle);
		user.save();
		return this;
	}
	
	public User addMessage(User sender, User receiver, String subject, String message) {
		Date date = new Date();
		Message newMessage = new Message(subject, message, date, sender, receiver);
		newMessage.save();
		sender.sentMessages.add(newMessage);
		receiver.receivedMessages.add(newMessage);
		sender.save();
		receiver.save();
		return this;
	}
	

	public DriverJourney addStartJourney(User user, Vehicle vehicle, String departurestate,
			String departuretownn, String departurestreet, Date departuredate,
			String departurehour, String arrivestate,
			String arrivetownn, String arrivestreet, String journeynotes,
			String expecteddistance, String expectedtime, boolean priceshare,
			Integer numberofhitchhiker){

		List<User> passengers = new ArrayList<User>();
		
		List<Request> requests = new ArrayList<Request>();
		
		Journey journey = new DriverJourney(user, vehicle, departurestate, departuretownn, departurestreet, departuredate,
				departurehour, arrivestate,
				arrivetownn, arrivestreet, journeynotes,
				expecteddistance, expectedtime, priceshare, numberofhitchhiker, passengers, requests);
		journey.save();
		journeyList.add(journey);
		user.save();
		return (DriverJourney) journey;
	}
	
	
	public HitchhikerJourney addStartHitcJourney(User user, String departurestate,
			String departuretownn, String departurestreet, Date departuredate,
			String departurehour, String arrivestate,
			String arrivetownn, String arrivestreet, String journeynotes, 
			boolean priceshare ){

		Journey journey = new HitchhikerJourney(user, departurestate, departuretownn, 
				departurestreet, departuredate, departurehour, arrivestate,
				arrivetownn, arrivestreet, journeynotes, priceshare );
		journey.save();
		journeyList.add(journey);
		user.save();
		return (HitchhikerJourney) journey;
	}


	public User addRequest(User user, User requester, DriverJourney journey) {
		Request newRequest = new Request(user, requester, journey, null, false);
		newRequest.save();
		user.comingRequests.add(newRequest);
		requester.sentRequests.add(newRequest);
		user.save();
		requester.save();
		
		return this;		
	}


	public User acceptRequest(Request request, DriverJourney journey) {
		journey.setNumberofhitchhiker(journey.getNumberofhitchhiker() - 1);
		List<User> pass = journey.getPassengers();
		User passenger = request.getRequester();
		pass.add(passenger);
		journey.setPassengers(pass);
		journey.save();
		request.setIsOk(true);
		request.setChecked(true);
		request.save();
		
		String subject = journey.getDeparturestate() + "-->" + journey.getArrivestate();
		String message = "Seyahat isteğiniz kabul edilmiştir...";
		Date date = new Date();
		User receiver = request.getRequester();
		
		Message newMessage = new Message(subject, message, date, this, receiver);
		newMessage.save();
		this.sentMessages.add(newMessage);
		receiver.receivedMessages.add(newMessage);
		this.save();
		receiver.save();
		
		return this;
	}


	public User rejectRequest(Request request, DriverJourney journey) {
		request.setIsOk(false);
		request.setChecked(true);
		request.save();
		
		String subject = journey.getDeparturestate() + "-->" + journey.getArrivestate();
		String message = "Seyahat isteğiniz reddedilmiştir...";
		Date date = new Date();
		User receiver = request.getRequester();
		
		Message newMessage = new Message(subject, message, date, this, receiver);
		newMessage.save();
		this.sentMessages.add(newMessage);
		receiver.receivedMessages.add(newMessage);
		this.save();
		receiver.save();
		
		return this;
	}

}
